package tts.dao;

// Generated 10-dic-2013 12.19.27 by Hibernate Tools 4.0.0

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tts.model.Users;
import tts.model.UsrDetails;

/**
 * Home object for domain model class Usrdetails.
 * @see .Usrdetails
 * @author Hibernate Tools
 */
@Repository("usrDetailsHome")
@Transactional(propagation = Propagation.REQUIRED)
public class UsrDetailsHome {

	//fields
	private static final Log log = LogFactory.getLog(UsrDetailsHome.class);
	@Autowired
	private UsersHome usersHome;
	@PersistenceContext
	private EntityManager entityManager;

	//get & set
	public UsersHome getUsersHome() {
		return usersHome;
	}
	public void setUsersHome(UsersHome usersHome) {
		this.usersHome = usersHome;
	}

	//methods

	public void insert(UsrDetails usrDetails, String username) {
		log.debug("persisting Usrdetails instance");
		try {
			
//			Users user = usersHome.findByUsername(username);
//			Users res = entityManager.find(Users.class, user.getUsers_id());
//			res.setUsrDetails(usrDetails);
			
//			usrDetails.setUsers(usersHome.findByUsername(username));
//			entityManager.merge(usrDetails);
			
//			Users user = usersHome.findByUsername(username);
//			usrDetails.setUsers(user);
//			user.setUsrDetails(usrDetails);
//			entityManager.merge(user);
			
//			Users user = usersHome.findByUsername(username);
//			usrDetails.setUsers(user);
//			entityManager.merge(usrDetails);
			
			Users user = usersHome.findByUsername(username);
			usrDetails.setUsers(user);
			entityManager.merge(usrDetails);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void edit(Users user, UsrDetails newUsrDetails) {
		log.debug("editing Usrdetails instance");
		try {
			UsrDetails usrDetails = this.findByUser(user);
			if (usrDetails==null) {
				newUsrDetails.setUsers(user);
				user.setUsrDetails(newUsrDetails);
				entityManager.persist(newUsrDetails);
			}
			else {
				usrDetails.setAddress(newUsrDetails.getAddress());
				usrDetails.setBirthdate(newUsrDetails.getBirthdate());
				usrDetails.setCellNumber(newUsrDetails.getCellNumber());
				usrDetails.setCity(newUsrDetails.getCity());
				usrDetails.setEmail(newUsrDetails.getEmail());
				usrDetails.setName(newUsrDetails.getName());
				usrDetails.setPhoneNumber(newUsrDetails.getPhoneNumber());
				usrDetails.setSurname(newUsrDetails.getSurname());
				usrDetails.setTaxCode(newUsrDetails.getTaxCode());
			}			
			log.debug("edit successful");
		} catch (RuntimeException re) {
			log.error("edit failed", re);
			throw re;
		}
	}

	public void delete(UsrDetails persistentInstance) {
		log.debug("removing Usrdetails instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public UsrDetails findById(Integer id) {
		log.debug("getting Usrdetails instance with id: " + id);
		try {
			UsrDetails instance = entityManager.find(UsrDetails.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public UsrDetails findByUser(Users user) {
		UsrDetails usrDetails = null;
		log.debug("getting Usrdetails instance for user: " + user.getUsername());
		try {
			List<UsrDetails> list = entityManager.createQuery("from UsrDetails where users_id="+user.getUsers_id()).getResultList();
			if (!list.isEmpty()) usrDetails=list.get(0); 
			log.debug("get successful");
			return usrDetails;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}